﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

using DotConferenceCore.GestionParticipant;
using DotConferenceCore.Logistique;
using DotConferenceCore.GestionArticle;
using DotConferenceSql;

namespace WebInterface
{
    public partial class EnvoyerSoumission : System.Web.UI.Page
    {
        public Auteur unAuteur;

        protected void Page_Load(object sender, EventArgs e)
        {
            unAuteur = new Auteur();

            //On va chercher l'enregistrement du Participant connecté
            using (var query = new DotConferenceSql.SqlQuery())
            {
                var selectPart = new DotConferenceSql.SelectQueryElement();

                selectPart.Add("NumeroParticipant");
                selectPart.Add("Prenom");
                selectPart.Add("Nom");

                query.Elements.Add(selectPart);
                query.Elements.Add(new DotConferenceSql.FromTableQueryElement("Participant"));

                query.Elements.Add(new DotConferenceSql.CustomWhereStatement(string.Format("NumeroParticipant={0}", MesVariablesSessions.Instance.no_participant_logger.ToString())));

                var selectCommand = query.BuildSqlCommand();

                try
                {
                    var dReader = selectCommand.ExecuteReader();

                    if (dReader != null)
                    {
                        while (dReader.Read())
                        {
                            unAuteur.Nom = string.Format("{0}", dReader["Nom"]);
                            unAuteur.Prenom = string.Format("{0}", dReader["Prenom"]);
                            unAuteur.NumeroParticipant = (int)dReader["NumeroParticipant"];
                            LblNom.Text = string.Format("{1} {0}", dReader["Prenom"], dReader["Nom"]);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Response.Write(ex.ToString());
                }
            }
        }

        protected void BtnSubmit_Click(object sender, EventArgs e)
        {
            DateTime datelimite;
            datelimite = DateTime.Now;

            //On doit aller chercher la date limite pour l'envoie d'une soumission
            //On va chercher l'enregistrement du Participant connecté
            using (var query = new DotConferenceSql.SqlQuery())
            {
                var selectPart = new DotConferenceSql.SelectQueryElement();

                selectPart.Add("ID_Conference");
                selectPart.Add("dateLimiteReceptionArticle");

                query.Elements.Add(selectPart);
                query.Elements.Add(new DotConferenceSql.FromTableQueryElement("Conference"));

                query.Elements.Add(new DotConferenceSql.CustomWhereStatement(string.Format("ID_Conference={0}", MesVariablesSessions.Instance.no_conference_active.ToString())));

                var selectCommand = query.BuildSqlCommand();

                try
                {
                    var dReader = selectCommand.ExecuteReader();

                    if (dReader != null)
                    {
                        while (dReader.Read())
                        {
                            datelimite = dReader.GetDateTime(dReader.GetOrdinal("dateLimiteReceptionArticle"));
                        }
                    }                      
                }
                catch (Exception ex)
                {
                    Response.Write(ex.ToString());
                }
            }
           
            if (DateTime.Now <= datelimite)
               { 
                    unAuteur.EnvoyerSoumission(TextSujet.Text, TextContenu.Text);
                    Response.Redirect("MenuAuteur.aspx");
               }
            else
                LblErreur.Text = "On ne peut soumettre un article après la date limite : " + datelimite.ToString() ;
        }

        protected void BtnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("MenuAuteur.aspx");
        }
    }
}
